import 'dart:convert';
import 'package:dio/dio.dart';

///统一接口返回格式错误检测
class BaseErrorInterceptors extends InterceptorsWrapper {
  BaseErrorInterceptors();

  @override
  onRequest(RequestOptions options) async {
    return options;
  }

  @override
  onError(DioError err) async {
    return err;
  }

  @override
  onResponse(Response response) async {
    var data = response.data;

    if (data is String) {
      try {
        data = json.decode(data);
      } catch (e) {
        print(e.toString());
      }
    }

    if (data is Map) {
      preDealRes(data);
    }

    return response;
  }

  ///预处理响应
  void preDealRes(Map<dynamic, dynamic> res) {}

  ///
  ///     ///在拿不到context的地方通过navigatorKey进行路由跳转：
  ///    ///https://stackoverflow.com/questions/52962112/how-to-navigate-without-context-in-flutter-app
  ///    ///navigatorKey.currentState.pushNamed(LoginWanandroidPage.ROUTER_NAME);
  ///
  void goLogin() {}
}
